package com.example.fashionshoes.controller;

import com.alibaba.fastjson2.JSONArray;
import com.example.fashionshoes.constant.RedisConst;
import com.example.fashionshoes.pojo.po.Goods;
import com.example.fashionshoes.pojo.po.Hot;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.service.GoodsService;
import com.example.fashionshoes.service.HotService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private GoodsService goodsService;
    @Resource
    private HotService hotService;
    /**
     * 根据参数从redis中获取不同轮播图图片
     * @param distributionSite 用于分辨键
     * @return 轮播图json数组
     */
    @GetMapping("/banner")
    public Result<JSONArray> getBanner(@Param("distributionSite") Integer distributionSite){
        String banner = stringRedisTemplate.opsForValue().get(RedisConst.BANNER+distributionSite);
        JSONArray parse = JSONArray.parse(banner);
        return Result.success(parse);
    }

    /**
     * 获取新鲜好物数据
     * @return 新鲜好物数据
     */
    @GetMapping("/new")
    public Result<List<Goods>> getNewGoods(){
        List<Goods> newList = goodsService.getNewGoods();
        return Result.success(newList);
    }

    /**
     * 获取人气推荐数据
     * @return 人气推荐数据
     */
    @GetMapping("/hot")
    public Result<List<Hot>> getHotGoods(){
        List<Hot> hotList =  hotService.list();
        return Result.success(hotList);
    }
}
